/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.una.accidentmanagement.bo;

import br.com.una.accidentmanagement.integration.Attendant;
import br.com.una.accidentmanagement.integration.Helpdesk;
import br.com.una.accidentmanagement.integration.Requester;
import br.com.una.accidentmanagement.integration.Ticket;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;

/**
 *
 * @author emilianoeloi
Padrão do XML
<?xml version='1.0' encoding='UTF-8' ?> 
<!-- was: no XML declaration present -->
<!-- Padrão de Importação Acordado em 20/03/2014 -->
<helpdesk>
    <!-- Áreas: BI, SO, CORP, BD -->
    <origem>SO</origem>
    <incidente>
        <atendente>
            <cpf>01365229637</cpf>
            <nome>João Junior</nome>
        </atendente>
        <solicitante>
            <cpf>01365229677</cpf>
            <nome>Maria Alice</nome>
        </solicitante>
        <titulo>Título</titulo>
        <descricao>
                <![CDATA[
                    Texto <xyz>
                ]]>
        </descricao>
        <abertura>Timestamp</abertura>
    </incidente>
</helpdesk>
 */
public class HelpdeskController {

    public List<br.com.una.accidentmanagement.model.Ticket> importTickets(String inputFile) throws JAXBException {
        File file = new File(inputFile);
        JAXBContext jaxbContext = JAXBContext.newInstance(Helpdesk.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Helpdesk helpdesk = (Helpdesk) jaxbUnmarshaller.unmarshal(file);
        
        List<br.com.una.accidentmanagement.model.Ticket> tickets = new LinkedList<br.com.una.accidentmanagement.model.Ticket>();
        
        for(Ticket integrationTicket : helpdesk.getTickets()){
            br.com.una.accidentmanagement.model.Ticket ticket = new br.com.una.accidentmanagement.model.Ticket();
            ticket.setCreated(integrationTicket.getCreated());
            ticket.setDescription(integrationTicket.getDescription());
            
            br.com.una.accidentmanagement.model.Requester requester = new br.com.una.accidentmanagement.model.Requester();
            requester.setCpf(integrationTicket.getRequester().getCpf());
            requester.setName(integrationTicket.getRequester().getName());
            
            br.com.una.accidentmanagement.model.Attendant attendant = new br.com.una.accidentmanagement.model.Attendant();
            attendant.setCpf(integrationTicket.getAttendant().getCpf());
            attendant.setName(integrationTicket.getAttendant().getName());
            
            ticket.setRequester(requester);
            tickets.add(ticket);
        }
        
        return tickets;
    }

    public void exportTickets(List<br.com.una.accidentmanagement.model.Ticket> tickets, String outputFile) throws PropertyException, JAXBException {
        Helpdesk helpdesk = new Helpdesk();
        helpdesk.setOrigin("SO");
        helpdesk.setTickets(new LinkedList<Ticket>());
        for (br.com.una.accidentmanagement.model.Ticket ticket : tickets) {
            Ticket integrationTicket = new Ticket();

            Requester requester = new Requester();
            requester.setCpf(ticket.getRequester().getCpf());
            requester.setName(ticket.getRequester().getName());
            integrationTicket.setRequester(requester);
            
            Attendant attendant = new Attendant();
            attendant.setCpf(ticket.getLastAttendant().getCpf());
            attendant.setName(ticket.getLastAttendant().getName());
            integrationTicket.setAttendant(attendant);

            integrationTicket.setTitle(ticket.getService().getSubject());
            integrationTicket.setDescription(ticket.getDescription());
            integrationTicket.setCreated(ticket.getCreated());

            helpdesk.getTickets().add(integrationTicket);


        }

        File file = new File(outputFile);
        JAXBContext jaxbContext = JAXBContext.newInstance(Helpdesk.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(helpdesk, file);
    }
}
